var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    nowtime: null,
    thisSelectIndex:100000,
    merchant:null, 
    isShow:true,  
    next: true,
    list: [],
    optinos:[],
    page: 0,
    size: 10,
    modleShow:0,  
    submitUrl:"/api/balance/balanceGetGoods.do",
    action:"/api/balance/productlist.do",
   	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      nowtime: new Date().getTime(),
      merchant: wx.getStorageSync("banrdId")
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isShow){
        this.setData({
          thisSelectIndex: 100000,
          next: true,
          list: [],
          optinos: [],
          page: 0,
          size: 10,
          modleShow: 0,  
          isShow:false
        })   
        this.getList();
   }
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { banrdId: self.data.merchant, pageNo: parseInt(self.data.page), pageSize: self.data.size }; 
    ux.request(self.data.action, params, function (data) {
      console.log(JSON.stringify(data))
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  onForm:function(event){
    if (this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  selectObtion: function (event) {//选择属性
    var dataset = event.currentTarget.dataset;
    var aindex = dataset.aindex;
    var bindex = dataset.bindex;
    var optins = this.data.list[this.data.thisSelectIndex].optioss; 
    
    
    var parent = optins[aindex].childs;
    for (var i = 0; i < parent.length; i++) {
      if(bindex==i){
        parent[i].desp ="select";
      }else{
        parent[i].desp = 1;
      }
    }
    var lists = this.data.list;
    lists[this.data.thisSelectIndex].optioss[aindex].childs=parent;
    var arra = lists[this.data.thisSelectIndex].optioss;
    var property="";
    for (var i = 0; i < arra.length; i++) {
      for (var j = 0; j < arra[i].childs.length; j++) {
          if (arra[i].childs[j].desp=="select"){
            property = property + arra[i].childs[j].name + " "
          }
    }
    }
    lists[this.data.thisSelectIndex].attribute=property;
    this.setData({
      list:lists,
      optinoss: lists[this.data.thisSelectIndex].optioss
    })
  },
  selectOptions: function (event){//点击商品选择属性按钮
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    if (this.data.modleShow==0){
      var optinoss = this.data.list[index].optioss;
         this.setData({
           thisSelectIndex:index,
           optinoss: optinoss,
           modleShow:1
         })
    } else if (this.data.modleShow == 1){
      this.setData({
        modleShow: 0
      })
    }
  },
  jianCount: function (event){
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    if (this.data.list[index].count==0) {
      return false
    } else {
      
      var lists = this.data.list;
      if (this.data.list[index].count == 1) {
        lists[index].attribute = "";
      }
      lists[index].count = lists[index].count - 1;
      lists[index].number = lists[index].number + 1;
      this.setData({
        list: lists
      })
    }
  },
  jiaCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    if (this.data.list[dataset.index].number==0){
        return false
    }else{
      var lists=this.data.list;
      lists[index].count = lists[index].count+1;
      lists[index].number = lists[index].number - 1;
      this.setData({
        list: lists
      })
    }
  },
  onTap:function(event){
    console.log("JSON.stringify(event)");
	  var dataset = event.currentTarget.dataset;
    var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isShow: true
    })  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getList();
  },
  panduan: function (optinoss) {
    var isDk = true;
    for (var i = 0; i < optinoss.length; i++) {
      var isflag = false;
      for (var j = 0; j < optinoss[i].childs.length; j++) {
        if (optinoss[i].childs[j].desp == "select") {
          isflag = true;
          break;
        }
      }
      if (!isflag) {
        isDk = false;
        break;
      }
    }
    return isDk;
  },
  tijiao:function(){
    var self=this;
    var lists=this.data.list;
    var isFlag=false;
    var tFlag=false;
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].count>0){
        tFlag=true;
        if (!lists[i].attribute || lists[i].attribute =="没有选择规格"){
          lists[i].attribute="没有选择规格";
           isFlag=true;
           }
      }
    }
    if (!isFlag&&!tFlag){
      this.toast("请选择提货数量");
      return false;
    }
   
    var listss = this.data.list;
    var ids="";
    var counts="";
    var pty="";
    for (var i = 0; i < listss.length; i++) {
      if (listss[i].count > 0 && listss[i].attribute)      {
        ids += listss[i].id+",";
        counts += listss[i].count + ",";
        pty += listss[i].attribute + ",";

      }
    }
    
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var params = { banrdId: self.data.merchant, ids: ids, counts: counts, pty: pty };
    console.log(JSON.stringify(params));
    ux.request(self.data.submitUrl, params, function (data) {
      if (data.success){
        wx.navigateTo({
          url: '/pages/member/tab2/confirmOrder?orderId='+data.data.orderId,
        })
      }else{
           wx.showToast({
             title: '提货失败',
           })
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });

  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }
})
